package yxy.game.pm2.bean.db.newDao;

import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.TaskPo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author HYB
 * Date 2021/12/7
 * Time 19:57
 */
public class TaskDAO {

    private static String getPrefix(String key) {
//        System.out.println("getPrefix:" + key);
        //如果存在这种格式的key：PLAYER:0vu4nw:MODULE:HEROINFO，则取出pcode的首位做分表
        return (key.indexOf("PLAYER:") == 0) ? key.split(":")[1].substring(0, 1) : key.substring(0, 1);
    }

    //表名
    private static String getTableName(String key) {
        return "task_" + getPrefix(key);
    }

    private static String replaceTableName(String sql, String key) {
        return sql.replace("{TABLE}", getTableName(key));
    }

    private static final String
            ADD_ONE = "replace into `{TABLE}` (`pCode`, `code`, `dataId`, `progress`, `finish`, `reward`, `extraParam`, `updateTime`) values (?,?,?,?,?,?,?,?)",
            SELECT_ALL = "select * from `{TABLE}` where `pCode` = ?",
            DELETE = "delete from `{TABLE}` where `pCode` = ? and `updateTime` != ?";

//    public static boolean replace(List<TaskPo> taskPos, long updateTime) {
//        List<Object[]> list = new ArrayList<>();
//        for (TaskPo taskPo : taskPos) {
//            Object[] objects = new Object[]{taskPo.getPlayerCode(), taskPo.getCode(), taskPo.getDataId(), taskPo.getProgress(), taskPo.isFinish(),
//            taskPo.isReward(),taskPo.getExtraParam(),updateTime};
//            list.add(objects);
//        }
//        int[] ints = DbManager.instance().executeBatchCommand(ADD_ONE, list);
//        return ints.length == taskPos.size();
//    }

    public static boolean replace(String playerCode, List<TaskPo> taskPos, long updateTime) {
        List<Object[]> list = new ArrayList<>();
        for (TaskPo taskPo : taskPos) {
            Object[] objects = new Object[]{taskPo.getPlayerCode(), taskPo.getCode(), taskPo.getDataId(), taskPo.getProgress(), taskPo.isFinish(),
                    taskPo.isReward(),taskPo.getExtraParam(),updateTime};
            list.add(objects);
        }
        String sql = replaceTableName(ADD_ONE, playerCode);
        System.out.println("TaskDAO:replace " + sql);
        int[] ints = DbManager.instance().executeBatchCommand(sql, list);
        return ints.length == taskPos.size();
    }

    public static HashMap<String, TaskPo> getMap(String pCode) {
        return getList(pCode).stream().collect(Collectors.toMap(TaskPo::getCode, e -> e, (a, b) -> b, HashMap::new));
    }

    public static List<TaskPo> getList(String pCode) {
        String sql = replaceTableName(SELECT_ALL, pCode);
        System.out.println("TaskDAO:getList " + sql);
        return DbManager.instance().executeQueryObjectList(sql, new Object[]{pCode}, TaskPo.builder);
    }

    public static void delete(String pCode, long updateTime) {
        String sql = replaceTableName(DELETE, pCode);
        System.out.println("TaskDAO:delete " + sql);
        DbManager.instance().executeCommand(sql, new Object[]{pCode, updateTime});
    }
}
